package cn.com.dycc.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.com.dycc.common.attach.entity.Attach;
import cn.com.dycc.common.attach.service.NotFoundAttachException;

/**
 * 文件工具类
 * @author gaofan
 *
 */
public class FileUtil {
	
	/**
	 * 返回文本每行数据信息数组
	 * @param filePath 文件路径
	 * @return
	 */
	public static String[] readTxt(String filePath){
		FileInputStream in=null;
		BufferedReader reader=null;
		StringBuilder builder=new StringBuilder();
		try {
			in=new FileInputStream(filePath);
			reader=new BufferedReader(new InputStreamReader(in, "utf-8"));
			
			String content=null;
			content=reader.readLine();
			while (content!=null) {
			
				if("".equals(builder.toString())){
					builder.append(content);
				}else{
					builder.append(","+content);
				}
				content=reader.readLine();
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return builder.toString().split(",");
	}
	
	/**
	 * 将临时目录下的文件移到上传目录下
	 * @param listAttach 附件信息集合
	 * @param module 所属模块
	 * @return
	 */
	public static List<Attach> moveFile(List<Attach> listAttach,String module){
		List<Attach> newListAttach=new ArrayList<Attach>();
		if(listAttach!=null&&listAttach.size()>0){
			//旧路径
			String old_path = null;
			//新路径
            String new_path = null;
			int byteread = 0;
			
            //后缀
            String attach_ext = null; 
            for(Attach aloneAttach : listAttach){
				try{
					//旧路径
					old_path = StringUtil.attachPath(aloneAttach.getAttach_path());
					
					//old_path=old_path.substring(old_path.indexOf("/")+1);
					String server_title = old_path.substring(old_path.lastIndexOf("/"),old_path.lastIndexOf("."));
									
					//后缀
					attach_ext = aloneAttach.getAttach_ext();
					//old_path+title+attach_ext
					InputStream inputStream = new FileInputStream(old_path);
					
					new_path = PathUtil.getPath()+module;
					
					File filePath = new File(new_path);
					if(!filePath.exists()){
						filePath.mkdirs();
					}
					String newFilePath=new_path+server_title+attach_ext;
					
					FileOutputStream fileOutputStream = new FileOutputStream(newFilePath);
					byte[] buffer = new byte[1444];
					while ((byteread = inputStream.read(buffer)) != -1){    
						fileOutputStream.write(buffer, 0, byteread);//三个参数，第一个参数是写的内容，  
			            //第二个参数是从什么地方开始写，第三个参数是需要写的大小  
			        }
					inputStream.close();  
			        fileOutputStream.close();
			        File old_file_Path = new File(old_path);
					if(old_file_Path.exists()){
						old_file_Path.delete();
					}
					//newFilePath=newFilePath.substring(newFilePath.indexOf("/")+1);
					
					aloneAttach.setAttach_path(newFilePath);
					newListAttach.add(aloneAttach);
				} catch (FileNotFoundException e) {
					throw new NotFoundAttachException("目标文件没有找到");
					
				} catch (IOException e) {
					throw new NotFoundAttachException("读取目标文件出现异常");
				}
            }
			return newListAttach;
		}else{
			return null;			
		}
		
	}
	
	
	
	public static String moveImage(String oldPath,String module){
		
		if(oldPath!=null&&oldPath.length()>0){
			//旧路径
			//String old_path = null;
			//新路径
            String new_path = null;
			int byteread = 0;
			
            //后缀
            String attach_ext = null; 
            
				try{
					//旧路径
					//old_path = StringUtil.attachPath(aloneAttach.getAttach_path());
					
					//old_path=old_path.substring(old_path.indexOf("/")+1);
					String server_title = oldPath.substring(oldPath.lastIndexOf("/"),oldPath.lastIndexOf("."));
									
					//后缀
					attach_ext = oldPath.substring(oldPath.lastIndexOf("."));
					//old_path+title+attach_ext
					InputStream inputStream = new FileInputStream(oldPath);
					
					new_path = PathUtil.getPath()+module;
					
					File filePath = new File(new_path);
					if(!filePath.exists()){
						filePath.mkdirs();
					}
					String newFilePath=new_path+server_title+attach_ext;
					
					FileOutputStream fileOutputStream = new FileOutputStream(newFilePath);
					byte[] buffer = new byte[1444];
					while ((byteread = inputStream.read(buffer)) != -1){    
						fileOutputStream.write(buffer, 0, byteread);//三个参数，第一个参数是写的内容，  
			            //第二个参数是从什么地方开始写，第三个参数是需要写的大小  
			        }
					inputStream.close();  
			        fileOutputStream.close();
			        File old_file_Path = new File(oldPath);
					if(old_file_Path.exists()){
						old_file_Path.delete();
					}
					//newFilePath=newFilePath.substring(newFilePath.indexOf("/")+1);
					
					//aloneAttach.setAttach_path(newFilePath);
					//newListAttach.add(aloneAttach);
					return newFilePath;
				} catch (FileNotFoundException e) {
					throw new NotFoundAttachException("目标文件没有找到");
					
				} catch (IOException e) {
					throw new NotFoundAttachException("读取目标文件出现异常");
				}            
			
		}else{
			return null;			
		}
		
	}
	
	public static String fileFound(String path){
		File file=new File(path);
		if(file.exists()){
			return "找到目标文件";
		}else{
			return "没有找到目标文件";
		}
	}

}
